<template>
  <template v-for="item in menuData">
    <!-- 没有子级展示 -->
    <el-menu-item
      @click="handleClick(item, `${props.index}-${item.meta.id}`)"
      :index="`${props.index}-${item.meta.id}`"
      v-if="!item.children || !item.children.length"
      :key="`${props.index}-${item.meta.id}`"
    >
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <!-- 有子级展示 -->
    <el-sub-menu v-else :index="`${props.index}-${item.meta.id}`">
      <template #title>
        <el-icon size="20">
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <!-- 组件递归 -->
      <tree-menu :menuData="item.children" :index="`${props.index}-${item.meta.id}`" />
      <!-- <el-menu-item-group v-for="children in item.children" :key="`${index}-${children.meta.id}`">
        <el-menu-item :index="`${index}-${children.meta.id}`">
          {{ children.meta.name }}
        </el-menu-item>
      </el-menu-item-group> -->
    </el-sub-menu>
  </template>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
const store = useStore()

const props = defineProps({
  menuData: Array, // 侧边栏数据
  index: [Number, String]
})
const router = useRouter()
const handleClick = (item, active) => {
  store.commit('addMenu', item.meta) // 把当前点击的子级页面存起来
  router.push({
    path: item.meta.path
  })
}
</script>
<style scoped lang="less"></style>
